package com.edu.app.service.impl.wechat;

import com.edu.app.exception.wechat.NotSupportHttpImpl;
import com.edu.app.service.sys.wechat.WechatBaseService;
import com.edu.app.util.crypto.SHA1;
import com.edu.app.util.http.DataType;
import com.edu.app.util.http.HttpImplConfig;
import com.edu.app.util.http.HttpInterface;
import com.edu.app.util.http.HttpUtils;
import com.edu.app.util.http.exception.NotSupportDataTypeException;
import com.edu.app.util.json.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WechatBaseServiceImpl implements WechatBaseService {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    HttpImplConfig httpImplConfig;

    public WechatBaseServiceImpl(HttpImplConfig httpImplConfig) {
        this.httpImplConfig = httpImplConfig;
    }

    /**
     * 获取short url
     */
    String SHORTURL_API_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
    @Override
    public String shortUrl(String longUrl)
    {
        String response = longUrl;
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("action", "long2short");
            map.put("long_url", longUrl);
            String strData = JsonUtil.toJson(map);
            map.clear();
            map.put("content",strData);

            HttpInterface httpInterface = HttpUtils.GetHttpInterface(httpImplConfig);
            response = httpInterface.HttpPost(SHORTURL_API_URL,map, DataType.JSON,"" );
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }catch(NotSupportHttpImpl e){
            throw new RuntimeException(e);
        }catch(NotSupportDataTypeException e){
            throw new RuntimeException(e);
        }
        return response;
    }


    @Override
    public boolean checkSignature(String token, String timestamp, String nonce, String signature) {
        try {
            String calStr =  SHA1.gen(timestamp, token, nonce);

             boolean b = calStr.equals(signature);
             return b;
        } catch (Exception e) {
            this.log.error("Checking signature failed, and the reason is :" + e.getMessage());
            return false;
        }
    }

    /**
     * 获取short url
     */
    String GET_AUTH_ACCESSTOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";
    @Override
    public String getAuthAccessToken(String oauthCode,
                                     String appid,
                                     String secret)
    {
        String response = "";
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("appid", appid);
            params.put("secret", secret);

            params.put("code",oauthCode);
            params.put("grant_type", "authorization_code");

            HttpInterface httpInterface = HttpUtils.GetHttpInterface(httpImplConfig);
            response = httpInterface.HttpGetSync(GET_AUTH_ACCESSTOKEN, params);

        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }catch(NotSupportHttpImpl e){
            throw new RuntimeException(e);
        }
        return response;
    }

}
